package com.example.myapplication;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;

public class InterestActivity extends AppCompatActivity {
    private Button btnBack;
    private Button btnConfirm;
    private CheckBox cbRead;
    private CheckBox cbMusic;
    private CheckBox cbSport;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_interest);

        // 初始化控件
        btnBack = findViewById(R.id.btn_back);
        btnConfirm = findViewById(R.id.btn_confirm);
        cbRead = findViewById(R.id.cb_read);
        cbMusic = findViewById(R.id.cb_music);
        cbSport = findViewById(R.id.cb_sport);

        // 返回按钮点击反馈
        btnBack.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        btnBack.setBackgroundColor(Color.DKGRAY);
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        btnBack.setBackgroundColor(Color.TRANSPARENT);
                        break;
                }
                return false;
            }
        });

        btnBack.setOnClickListener(v -> finish());

        // 确定按钮点击反馈
        btnConfirm.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        btnConfirm.setBackgroundColor(Color.DKGRAY);
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        btnConfirm.setBackgroundColor(Color.TRANSPARENT);
                        break;
                }
                return false;
            }
        });

        btnConfirm.setOnClickListener(v -> returnInterest());
    }

    // 封装回传数据的方法
    private void returnInterest() {
        ArrayList<String> selectedInterests = new ArrayList<>();

        if (cbRead.isChecked()) {
            selectedInterests.add("阅读");
        }
        if (cbMusic.isChecked()) {
            selectedInterests.add("音乐");
        }
        if (cbSport.isChecked()) {
            selectedInterests.add("运动");
        }

        if (selectedInterests.isEmpty()) {
            Toast.makeText(this, "请选择至少一个兴趣", Toast.LENGTH_SHORT).show();
            return;
        }

        // 将选中的兴趣拼接成字符串
        String interest = String.join("、", selectedInterests);

        // 创建 Intent 携带数据
        Intent data = new Intent();
        data.putExtra("select_interest", interest);

        setResult(RESULT_OK, data);
        finish();
    }
}